import {request} from './request'

const Login = (data) => {
    const csrf_token = uni.getStorageSync('csrfmiddlewaretoken')
    return request('user_login/', {
        headers: {
            "X-CSRFToken": csrf_token,
        },
        method: 'POST',
        data: data
    })
}

const getCsrfToken = () => {
    return request('get-csrf-token/',  {method: "GET"}).then(res => {
        uni.setStorageSync('csrfmiddlewaretoken', res.csrfmiddlewaretoken)
    })
}

const ChatSession = (method, chatID) => {
	const userinfo = uni.getStorageSync('user')
	return request('chatsession/',  {
		method: method,
		headers: {
			"JSONWebToken": userinfo.token,
			"userID": userinfo.info.userID
		},
		data: {
			chatID: method === "delete" ? chatID : ''
		}
	})
}

const getChatSession = () => {
	const userinfo = uni.getStorageSync('user')
	return request('getchatsession/',  {
		headers: {
			"JSONWebToken": userinfo.token,
			"userID": userinfo.info.userID
		}
	})
}

const getChatHistory = (chatID) => {
	const userinfo = uni.getStorageSync('user')
	return request('getchathistory/',  {
		method: 'POST',
		headers: {
			"JSONWebToken": userinfo.token,
			"userID": userinfo.info.userID
		},
		data: {
			chatID: chatID
		}
	})
}

const getuserprofile = () => {
	const userinfo = uni.getStorageSync('user')
	return request('getuserprofile/',  {
		headers: {
			"JSONWebToken": userinfo.token,
			"userID": userinfo.info.userID
		},
	})
}

const getmasklist = () => {
	const userinfo = uni.getStorageSync('user')
	return request('getmasklist/', {
		headers: {
			"JSONWebToken": userinfo.token,
			"userID": userinfo.info.userID
		},
	})
}

const addSessionWithMask = (maskid) => {
	const userinfo = uni.getStorageSync('user')
	return request('add_session_with_mask/', {
		headers: {
			"JSONWebToken": userinfo.token,
			"userID": userinfo.info.userID
		},
		method: 'POST',
		data: {
			mask: maskid
		}
	})
}

module.exports = {
    Login,
	getuserprofile,
    getCsrfToken,
	ChatSession,
	getChatSession,
	getChatHistory,
	getmasklist,
	addSessionWithMask
}